package com.zbkj.crmeb.front.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.constants.ExpireTimeConstants;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.enums.BrandTypeEnum;
import com.zbkj.crmeb.front.vo.BrandVo;
import com.zbkj.crmeb.store.model.StoreBrand;
import com.zbkj.crmeb.store.service.StoreBrandService;
import com.zbkj.crmeb.store.service.StoreProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户表 前端控制器
 */
@Slf4j
@RestController("BrandFrontController")
@RequestMapping("api/front/brand")
@Api(tags = "品牌服务")
public class BrandController {
    @Autowired
    StoreBrandService storeBrandService;

    @Autowired
    private StoreProductService storeProductService;

    @Autowired
    RedisUtil redisUtil;
    public static final String REDIS_KEY_BRANDLIST = "rds_key_brand_list";

    /**
     * 城市服务树形结构数据
     */
    @ApiOperation(value = "品牌列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<BrandVo>> listBrand() {
        List<BrandVo> voList = (List<BrandVo>) redisUtil.get(REDIS_KEY_BRANDLIST);
        if (CollectionUtils.isEmpty(voList)) {
            //获取所有上架商品的品牌id
            List<Integer> brandIdList = storeProductService.getAllStoreProductBrand();
            List<StoreBrand> list = storeBrandService.list(Wrappers.<StoreBrand>lambdaQuery()
                    .eq(StoreBrand::getStatus, true)
                    .eq(StoreBrand::getType, BrandTypeEnum.XUNWU)
                    .in(StoreBrand::getId, brandIdList)
                    .orderByAsc(StoreBrand::getLetter)
                    );
            if (CollectionUtils.isNotEmpty(list)) {
                voList = list.stream().map(brand -> new BrandVo(brand.getId(), brand.getEnName(), brand.getLetter(), brand.getLogo()))
                        .sorted((x, y) -> {
                            String brandNameX = x.getName();
                            String brandNameY = y.getName();
                            //小写转大写
                            if (brandNameX.matches(".*?[a-z]+.*?")){
                                brandNameX = brandNameX.toUpperCase();
                            }
                            if (brandNameY.matches(".*?[a-z]+.*?")){
                                brandNameY = brandNameY.toUpperCase();
                            }
                            return brandNameX.compareTo(brandNameY);
                        }).collect(Collectors.toList());

                redisUtil.set(REDIS_KEY_BRANDLIST, voList, ExpireTimeConstants.HALF_HOUR, TimeUnit.SECONDS);
            } else {
                voList = new ArrayList<>();
            }
          }
            return CommonResult.success(voList);
        }
}



